package core

import (
	"os"
	"path/filepath"
	"strings"
	"time"
)

func getCurrentExeDir() string {
	dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
	return strings.Replace(dir, "\\", "/", -1)
}

// checkDir 创建目录
func checkDir(outDir string) {
	if _, er2 := os.Stat(outDir); os.IsNotExist(er2) {
		_ = os.MkdirAll(outDir, 0777)
		_ = os.Chmod(outDir, 0777)
	}
	return
}

func BackFile(fileName string, fileData []byte) error {
	timeNow := time.Now()
	monthStr := timeNow.Format("200601")
	fileFullDir := getCurrentExeDir() + "/bak_files/" + monthStr
	checkDir(fileFullDir)
	fileFullName := fileFullDir + "/" + fileName
	err := os.WriteFile(fileFullName, fileData, 0666)
	return err
}
